package com.yfqy.app.modules.order.factory;

import com.yfqy.app.enums.OrderType;
import com.yfqy.app.modules.order.director.AbstractOrderCreateBuilderDirector;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class OrderCreateBuilderFactory {
    private final Map<OrderType, AbstractOrderCreateBuilderDirector> serviceMap;

    public OrderCreateBuilderFactory(List<AbstractOrderCreateBuilderDirector> services) {
        this.serviceMap = services.stream()
                .collect(Collectors.toMap(
                        AbstractOrderCreateBuilderDirector::getOrderType,
                        Function.identity()
                ));
    }

    public AbstractOrderCreateBuilderDirector getService(OrderType orderType) {
        AbstractOrderCreateBuilderDirector service = serviceMap.get(orderType);
        if (service == null) {
            throw new IllegalArgumentException("不支持的订单类型: " + orderType);
        }
        return service;
    }
}
